package com.demo.lucene.index;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


/**
 * 索引
 *
 * @author lee9213@163.com
 * @version 1.0
 * @date 2017/3/14 11:35
 */
public class Indexer {

    private static final String indexPath = "";

    /**
     * 创建索引
     * @throws IOException
     */
    public static void create() throws IOException {
        Directory directory = FSDirectory.open(new File(indexPath));
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
        Document document = new Document();
        document.add(new Field("name","zhangsan", Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("address","sichuan", Field.Store.YES, Field.Index.ANALYZED));
        document.add(new Field("sex","man", Field.Store.YES, Field.Index.NOT_ANALYZED));
        document.add(new Field("introduce","i am a coder", Field.Store.YES, Field.Index.NO));
        IndexWriter indexWriter = new IndexWriter(directory,analyzer, IndexWriter.MaxFieldLength.LIMITED);
        indexWriter.addDocument(document);
        //删除索引
        indexWriter.deleteDocuments(new Term("name","zhangsan"));
        //更新索引
        indexWriter.updateDocument(new Term("name","zhangsan"),document);
        indexWriter.close();
    }
    public static void search() throws ParseException, IOException {
        String queryString = "zhangsan";
        String[] fields = {"name","introduce"};
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
        QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_20,fields,analyzer);
        Query query = queryParser.parse(queryString);
        IndexSearcher indexSearcher = new IndexSearcher(indexPath);
        Filter filter = null;
        TopDocs topDocs = indexSearcher.search(query,filter,10000);
        System.out.println("hits :"+topDocs.totalHits);

        for(ScoreDoc scoreDoc : topDocs.scoreDocs){
            int docNum = scoreDoc.doc;
            Document document = indexSearcher.doc(docNum);
            printDocumentInfo(document);
        }

        //查询name=zhangsan
        Term term = new Term("name","zhangsan");
        Query termQuery = new TermQuery(term);
        //查询2到100条包含size字段
        NumericRangeQuery numericRangeQuery = NumericRangeQuery.newIntRange("size",2,100,true,true);
        //查询通配符，模糊查询
        Term wildcardTerm = new Term("name","zhangsa?");
        WildcardQuery wildcardQuery = new WildcardQuery(wildcardTerm);
        //查询content中dog和cat之间距离不超过5个单词的数据
        PhraseQuery phraseQuery = new PhraseQuery();
        phraseQuery.add(new Term("content","dog"));
        phraseQuery.add(new Term("content","cat"));
        phraseQuery.setSlop(5);

        //组合查询
        PhraseQuery phraseQuery1 = new PhraseQuery();
        phraseQuery1.add(new Term("content","dog"));
        phraseQuery1.add(new Term("content","cat"));
        phraseQuery1.setSlop(5);

        Term wildcardTerm1 = new Term("name","zhansan?");
        WildcardQuery wildcardQuery1 = new WildcardQuery(wildcardTerm1);

        BooleanQuery booleanQuery = new BooleanQuery();
        booleanQuery.add(phraseQuery1, BooleanClause.Occur.MUST);
        booleanQuery.add(wildcardQuery1, BooleanClause.Occur.MUST);
    }

    public static void sort() throws ParseException, IOException {
        String queryStr = "zhang";
        String[] fields = {"name","address","size"};
        Sort sort = new Sort();
        SortField sortField = new SortField("size",SortField.INT,true);
        sort.setSort(sortField);
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_20);
        QueryParser queryParser = new MultiFieldQueryParser(fields,analyzer);
        Query query = queryParser.parse(queryStr);
        IndexSearcher indexSearcher = new IndexSearcher(indexPath);
        Filter filter = null;
        TopDocs topDocs = indexSearcher.search(query,filter,100,sort);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs){
            int docNum = scoreDoc.doc;
            Document document = indexSearcher.doc(docNum);
            printDocumentInfo(document);
        }
    }
    private static void printDocumentInfo(Document document){

    }

}